#define _CRT_SECURE_NO_WARNINGS 1
//#include<stdio.h>
//#include<stdlib.h>
//struct monster
//{
//	int name;
//	int power;
//	int life;
//};
//void print1(int* p, int p_sz)
//{
//	int i = 0;
//	for (;i<p_sz;i++)
//	{
//		printf("%d ",*(p+i));
//	}
//	printf("\n");
//}
//void print2(struct monster*p, int p_sz)
//{
//	int i = 0;
//	for (; i < p_sz; i++)
//	{
//		printf("%d ", p->life);
//		p++;
//	}
//	printf("\n");
//}
//void print3(struct monster* p, int p_sz)
//{
//	int i = 0;
//	for (; i < p_sz; i++)
//	{
//		printf("%s ", p->name);
//		p++;
//	}
//	printf("\n");
//}
//
//int int_cmp(const void*i1,const void*i2)
//{
//	return(*(int*)i1 - *(int*)i2);
//}
//int str_cmp_life(const void*s1_life,const void*s2_life)
//{
//	return(((struct monster*)s1_life)->life-((struct monster*)s2_life)->life);
//}
//int str_cmp_name(const void* s1_name,const void* s2_name)
//{
//	return strcmp(((struct monster*)s1_name)->name, ((struct monster*)s2_name)->name);
//}
//int main()
//{
//	int arr1[10] = { 1,2,6,4,5,3,9,8,7,10 };
//	int sz1 = sizeof(arr1) / sizeof(arr1[0]);
//	print1(arr1, sz1);
//	qsort(arr1, sz1, sizeof(arr1[0]),int_cmp);
//	print1(arr1, sz1);
//	printf("\n\n");
//	struct monster arr2[3] = { {"goblin",10,100},{"wolf",15,60},{"savage",12,150}};
//	int sz2 = sizeof(arr2) / sizeof(arr2[0]);
//	print2(arr2, sz2);
//	qsort(arr2,sz2,sizeof(arr2[0]),str_cmp_life);
//	print2(arr2,sz2);
//	printf("\n\n");
//	print3(arr2, sz2);
//	qsort(arr2, sz2, sizeof(arr2[0]), str_cmp_name);
//	print3(arr2, sz2);
//
//}
#include<stdio.h>
#include<stdlib.h>
struct monster
{
	int name;
	int power;
	int life;
};
int int_cmp(const void*i1,const void*i2)
{
	return(*(int*)i1 - *(int*)i2);
}
int str_cmp_life(const void*s1_life,const void*s2_life)
{
	return(((struct monster*)s1_life)->life-((struct monster*)s2_life)->life);
}
int str_cmp_name(const void* s1_name,const void* s2_name)
{
	return strcmp(((struct monster*)s1_name)->name, ((struct monster*)s2_name)->name);
}
void print1(int* p, int p_sz)
{
	int i = 0;
	for (;i<p_sz;i++)
	{
		printf("%d ",*(p+i));
	}
	printf("\n");
}
void print2(struct monster*p, int p_sz)
{
	int i = 0;
	for (; i < p_sz; i++)
	{
		printf("%d ", p->life);
		p++;
	}
	printf("\n");
}
void print3(struct monster* p, int p_sz)
{
	int i = 0;
	for (; i < p_sz; i++)
	{
		printf("%s ", p->name);
		p++;
	}
	printf("\n");
}
void swap(char*x,char*y,int sz)
{
	int i = 0;
	for (;i<sz;i++)
	{
		char tmp = *(x+i);
		*(x+i) = *(y+i);
		*(y+i) = tmp;
	}
}
void bubble_sort(void*base,int num,int b_sz,int (*cmp)(const void* ,const void*))
{
	int i = 0;
	for (i = 0; i < num-1; i++)
	{
		int j = 0;
		for (j = 0; j < num - 1 - i; j++)
		{
			if (cmp((char*)base+b_sz*j,(char*)base+b_sz*(j+1))>0)
			{
				swap((char*)base+b_sz*j,(char*)base+b_sz*(j+1),b_sz);
			}
		}
	}
}
int main()
{
	int arr1[10] = { 1,2,6,4,5,3,9,8,7,10 };
	int sz1 = sizeof(arr1) / sizeof(arr1[0]);
	print1(arr1, sz1);
	bubble_sort(arr1, sz1, sizeof(arr1[0]),int_cmp);
	print1(arr1, sz1);
	printf("\n\n");
	struct monster arr2[3] = { {"goblin",10,100},{"wolf",15,60},{"savage",12,150}};
	int sz2 = sizeof(arr2) / sizeof(arr2[0]);
	print2(arr2, sz2);
	bubble_sort(arr2,sz2,sizeof(arr2[0]),str_cmp_life);
	print2(arr2,sz2);
	printf("\n\n");
	print3(arr2, sz2);
	bubble_sort(arr2, sz2, sizeof(arr2[0]), str_cmp_name);
	print3(arr2, sz2);
}